import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'
import { isLogin } from '@/utils/business'
import { isEmpty, assign } from 'lodash'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      redirect: '/home',
      component: Layout,
      children: [
        {
          path: '/login',
          name: 'login',
          component: () => import('../views/login/index.vue')
        },
        {
          path: '/noPermission',
          name: 'noPermission',
          component: () => import('../views/no-permission/index.vue')
        },
        {
          path: '/home',
          name: 'home',
          component: () => import('../views/home/index.vue')
        }
      ]
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'layout',
      component: Layout
    }
  ]
})
// 做登录拦截
router.beforeEach((to, from, next) => {
  if (history.state && isEmpty(history.state?.current)) {
    assign(history.state, { current: from.fullPath })
  }

  if (isLogin || to.path == '/login') {
    next()
  } else {
    next('/login')
  }
})

export default router
